﻿//Write a method that adds two positive integer numbers represented as arrays of digits
//(each array element arr[i] contains a digit; the last digit is kept in arr[0]). 
//Each of the numbers that will be added could have up to 10 000 digits

using System;
class SumTwoNumbers
{
    static void Main()
    {
        Console.WriteLine("Please, enter the first number");
        string firstNumber = Console.ReadLine();
        Console.WriteLine("Please, enter the second number");
        string secondNumber = Console.ReadLine();

        Console.WriteLine("The sum is: {0}", SumTwoPositiveNumbers(firstNumber, secondNumber));
    }


    static string SumTwoPositiveNumbers(string firstNumber, string secondNumber)
    {
        int[] firstArray = new int[firstNumber.Length];  //creating two arrays for number's digits
        int[] secondArray = new int[secondNumber.Length];

        int biggestLength = Math.Max(firstArray.Length, secondArray.Length); //the greater length
        int smallestLength = Math.Min(firstArray.Length, secondArray.Length);
        int[] sum = new int[biggestLength];
        int iThElementsSum = 0;

        for (int i = 0; i < firstNumber.Length; i++) //fill the first array with string's chars
        {
            firstArray[i] = firstNumber[i] - 48; //-48 - to take the right number from the char code
        }
        Array.Reverse(firstArray); //reversing the array because of the condition arr[0] = last digit

        for (int i = 0; i < secondNumber.Length; i++) //same procedure for the second array
        {
            secondArray[i] = secondNumber[i] - 48;
        }
        Array.Reverse(secondArray);

        sum[0] = (firstArray[0] + secondArray[0]) % 10;
        for (int i = 1; i < smallestLength; i++)
        {
            iThElementsSum = firstArray[smallestLength - 1] + secondArray[smallestLength - 1]; //the last sum, we'll use it in case the arrays are not equal
            sum[i] = (firstArray[i] + secondArray[i]) % 10 + (firstArray[i - 1] + secondArray[i - 1]) / 10;
        }
        Array.Reverse(sum);

        if (firstArray.Length > secondArray.Length) //the case when the digits in the arrays are not equal, add the digits from the  bigger number
        {
            Array.Reverse(firstArray);
            for (int i = 0; i < firstArray.Length - secondArray.Length; i++)
            {
                sum[i] = firstArray[i];
            }
            if (iThElementsSum >= 10)
            {
                sum[firstArray.Length - secondArray.Length - 1] = firstArray[firstArray.Length - secondArray.Length - 1] + (iThElementsSum / 10);
            }

            if (sum[firstArray.Length - secondArray.Length] + firstArray[firstArray.Length - secondArray.Length] >= 10)
            {
                int lastSumbiggerThanTen = sum[firstArray.Length - secondArray.Length] + firstArray[firstArray.Length - secondArray.Length];
                sum[firstArray.Length - secondArray.Length] = lastSumbiggerThanTen / 10;
            }
        }
        if (firstArray.Length < secondArray.Length)
        {
            Array.Reverse(secondArray);
            for (int i = 0; i < secondArray.Length - firstArray.Length; i++)
            {
                sum[i] = secondArray[i];
            }
            if (iThElementsSum >= 10)
            {
                sum[secondArray.Length - firstArray.Length - 1] = secondArray[secondArray.Length - firstArray.Length - 1] + (iThElementsSum / 10);
            }
            if (sum[secondArray.Length - firstArray.Length] + secondArray[secondArray.Length - firstArray.Length] >= 10)
            {
                int lastSumBiggerThanTen = sum[secondArray.Length - firstArray.Length] + secondArray[secondArray.Length - firstArray.Length];
                sum[secondArray.Length - firstArray.Length] = lastSumBiggerThanTen / 10;
            }
        }

        return string.Join("", sum);
    }
}
